﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CSharpExt.IO
{
    public class CountedStream : Stream
    {
        private Stream _stream;
        private long _readed;
        private long _writed;

        public CountedStream(Stream stream)
        {
            _stream = stream;
        }

        public override void Flush()
        {
            _readed = _writed = 0;
            _stream.Flush();
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            return _stream.Seek(offset, origin);
        }

        public override void SetLength(long value)
        {
            _stream.SetLength(value);
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            int readed = _stream.Read(buffer, offset, count);
            _readed += readed;
            return readed;
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            _writed += count;
            _stream.Write(buffer, offset, count);
        }

        public override void WriteByte(byte value)
        {
            ++_writed;
            _stream.WriteByte(value);
        }

        public override bool CanRead
        {
            get { return _stream.CanRead; }
        }

        public override bool CanSeek
        {
            get { return _stream.CanSeek; }
        }

        public override bool CanWrite
        {
            get { return _stream.CanWrite; }
        }

        public override long Length
        {
            //get { return _stream.Length; }
            get { return _writed; }
        }

        public override long Position
        {
            get { return _stream.Position; }
            set { _stream.Position = value; }
        }

        public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, Object state)
        {
            return _stream.BeginRead(buffer, offset, count, callback, state);
        }

        public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, Object state)
        {
            return _stream.BeginWrite(buffer, offset, count, callback, state);
        }

        public override bool CanTimeout
        {
            get
            {
                return _stream.CanTimeout;
            }
        }

        public override void Close()
        {
            if(_stream == null)
                return;

            _stream.Close();
            _readed = _writed = 0;
            _stream = null;
        }

        public override System.Runtime.Remoting.ObjRef CreateObjRef(Type requestedType)
        {
            return _stream.CreateObjRef(requestedType);
        }

        public override int EndRead(IAsyncResult asyncResult)
        {
            return _stream.EndRead(asyncResult);
        }

        public override void EndWrite(IAsyncResult asyncResult)
        {
            _stream.EndWrite(asyncResult);
        }

        public override int ReadByte()
        {
            ++_readed;
            return _stream.ReadByte();
        }

        public override int ReadTimeout
        {
            get
            {
                return _stream.ReadTimeout;
            }
            set
            {
                _stream.ReadTimeout = value;
            }
        }

        public override int WriteTimeout
        {
            get
            {
                return _stream.WriteTimeout;
            }
            set
            {
                _stream.WriteTimeout = value;
            }
        }

        public override bool Equals(object obj)
        {
            return _stream.Equals(obj);
        }

        public override int GetHashCode()
        {
            return _stream.GetHashCode();
        }

        public override object InitializeLifetimeService()
        {
            return _stream.InitializeLifetimeService();
        }

        public override string ToString()
        {
            return _stream.ToString();
        }

        public long ReadedBytes
        {
            get { return _readed; }
        }

        public long WritedBytes
        {
            get { return _writed; }
        }
    }

}
